const config = require('../../config.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    cartInfos:[],
    scrollHeight:'',
    totalPrice:0,
    showGoods:false,
    showContents:false
  }, 
  radioChange: function (e) {
    console.log('checkbox发生change事件，携带value值为：', e.detail)
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   var that = this;
    that.setData({
      scrollHeight: wx.getSystemInfoSync().windowHeight-100
    });
    
  },
 minusShopCart:function(e){
   var that =this;
   var userInfo = getApp().globalData.userInfo;
   var goodsId = e.currentTarget.dataset.id;
   wx.request({
     url: config.requestUrl + '/order/minusCart',
     header: { "token": userInfo.xOpenid },
     data:{"goodsId":goodsId},
     success(){
       wx.request({
         url: config.requestUrl + '/order/getCartInfo',
         header: { "token": userInfo.xOpenid },
         success(res) {
           if (res.data.data.length > 0) {
             var totalPrice = 0;
             for (var i = 0; i < res.data.data.length; i++) {
               var cartInfo = res.data.data[i];
               console.log(cartInfo.status);
               if (cartInfo.status == 1) {
                 totalPrice = totalPrice + cartInfo.price * cartInfo.amount;
               }
             }
             that.setData({ "cartInfos": res.data.data, "totalPrice": totalPrice, "showGoods": true });
           } else {
             that.setData({ "showGoods": false });
           }
         }
       });
     }
   });

  }, addShopCart: function (e) {
    var that = this;
    var userInfo = getApp().globalData.userInfo;
    var goodsId = e.currentTarget.dataset.id;
    wx.request({
      url: config.requestUrl + '/order/addCart',
      header: { "token": userInfo.xOpenid },
      data: { "goodsId": goodsId },
      success() {
        wx.request({
          url: config.requestUrl + '/order/getCartInfo',
          header: { "token": userInfo.xOpenid },
          success(res) {
            if (res.data.data.length > 0) {
              var totalPrice = 0;
              for (var i = 0; i < res.data.data.length; i++) {
                var cartInfo = res.data.data[i];
                console.log(cartInfo.status);
                if (cartInfo.status == 1) {
                  totalPrice = totalPrice + cartInfo.price * cartInfo.amount;
                }
              }
              that.setData({ "cartInfos": res.data.data, "totalPrice": totalPrice, "showGoods": true });
            } else {
              that.setData({ "showGoods": false });
            }
          }
        });
      }
    });

  }, deleteShopCart: function (e) {
    var that = this;
    var userInfo = getApp().globalData.userInfo;
    var goodsId = e.currentTarget.dataset.id;
    wx.showModal({
      title: '提示',
      content: '确定删除吗',
      success(res){
        if(res.confirm){
          wx.request({
            url: config.requestUrl + '/order/deleteCart',
            header: { "token": userInfo.xOpenid },
            data: { "goodsId": goodsId },
            success() {
              wx.request({
                url: config.requestUrl + '/order/getCartInfo',
                header: { "token": userInfo.xOpenid },
                success(res) {
                  if (res.data.data.length > 0) {
                    var totalPrice = 0;
                    for (var i = 0; i < res.data.data.length; i++) {
                      var cartInfo = res.data.data[i];
                      console.log(cartInfo.status);
                      if (cartInfo.status == 1) {
                        totalPrice = totalPrice + cartInfo.price * cartInfo.amount;
                      }
                    }
                    that.setData({ "cartInfos": res.data.data, "totalPrice": totalPrice, "showGoods": true });
                  } else {
                    that.setData({ "showGoods": false });
                  }
                }
              });
            }
          });
        }
      }
    })
   

  },
 toOrder:function(){
    wx.navigateTo({
      url: '/pages/order/order',
    })
   
 },
  toIndex: function () {
    wx.switchTab({
      url: '/pages/index/index',
    })

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var userInfo = getApp().globalData.userInfo;
    var that = this;
    wx.showLoading({
      title: '加载中',
    })
    setTimeout(function () {
      wx.hideLoading();
      that.setData({"showContents":true});
    }, 500);
    wx.request({
      url: config.requestUrl + '/order/getCartInfo',
      header:{"token":userInfo.xOpenid},
      success(res) {
        if (res.data.data.length>0){
        var totalPrice = 0;
        for (var i = 0; i < res.data.data.length; i++) {
          var cartInfo = res.data.data[i];
          console.log(cartInfo.status);
          if (cartInfo.status == 1) {
            totalPrice = totalPrice + cartInfo.price * cartInfo.amount;
          }
        }
        that.setData({ "cartInfos": res.data.data, "totalPrice": totalPrice,"showGoods":true});
        }else{
          that.setData({"showGoods":false});
        }
      }
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})